package com.yyfax.ph.common.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 图片和文字都居中的RadioButton
 */
public class DrawableCenterRadioButton extends TextView {
	public DrawableCenterRadioButton(Context context) {
		super(context);
	}

	public DrawableCenterRadioButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public DrawableCenterRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public DrawableCenterRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Drawable[] drawables = getCompoundDrawables();
		Drawable drawableLeft = drawables[0];
		if (drawableLeft != null) {
			float textWidth = getPaint().measureText(getText().toString());
			int drawablePadding = getCompoundDrawablePadding();
			int drawWidth = drawableLeft.getIntrinsicWidth();
			float bodyWidth = textWidth + drawablePadding + drawWidth;//text的宽度+drawable的宽度+drawable的padding
			setPadding((int) ((getWidth() - bodyWidth) / 2), getPaddingTop(), getPaddingRight(), getPaddingBottom());
			//canvas.translate((getWidth()-bodyWidth)/2,0);//向右移动到中心
		}
		super.onDraw(canvas);
	}
}
